package com.ld.common.util.notice.plugin;

import com.ld.common.plugin.Plugin;
import com.ld.common.plugin.PluginInfo;
import com.ld.common.util.notice.NoticeManager;
import com.ld.common.util.notice.NoticeModel;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.core.util.notice.mail.EmailInfo;
import com.ld.shieldsb.common.core.util.notice.mail.EmailUtil;

public class EmailNoticeUtil implements Plugin {
    private PluginInfo pluginInfo = new PluginInfo("邮件通知插件", this); // 插件信息
    public static final String NOTICE_TYPE = "emailNotice";

    public static final NoticeModel NOTICE = new NoticeModel("邮件通知", NOTICE_TYPE) {

        @Override
        public Result notice(String from, String sendTo, String subject, String content, Object otherParams) {
            content = content.replaceAll("\r\n", "<br>"); // 替换换行
            EmailInfo mailInfo = new EmailInfo();
            mailInfo.setFromName(from); // 不需要覆盖默认值时传null
            mailInfo.addToAddress(sendTo);
            mailInfo.setSubject(subject);
            mailInfo.setContent(content);
            return EmailUtil.sendEmail(mailInfo);
        }

    };

    @Override
    public void initializePlugin() {
        NoticeManager.register(NOTICE); // 注册到消息通知管理器中
    }

    @Override
    public void destroyPlugin() {

    }

    @Override
    public PluginInfo getPluginInfo() {
        return pluginInfo;
    }

}
